package com.example.menhoo.toollibrary.common.utils;

import com.example.menhoo.toollibrary.common.io.FileUtils;

import java.text.DecimalFormat;
import java.util.UUID;

/**
 * @author MaTianyu
 * @date 2014-11-21
 */
public class NumberUtil {

    /**
     * 返回只保留2位小数的字符串格式
     * @param val
     * @return
     */
    public static String float2ToString(float val){
        DecimalFormat decimalFormat = new DecimalFormat("##.##");
        return decimalFormat.format(val);
    }
    public static int convertToint(String intStr, int defValue) {
        try {
            return Integer.parseInt(intStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return defValue;
    }

    public static long convertTolong(String longStr, long defValue) {
        try {
            return Long.parseLong(longStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return defValue;
    }

    public static float convertTofloat(String fStr, float defValue) {
        try {
            return Float.parseFloat(fStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return defValue;
    }

    public static double convertTodouble(String dStr, double defValue) {
        try {
            return Double.parseDouble(dStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return defValue;
    }


    public static Integer convertToInteger(String intStr) {
        try {
            return Integer.parseInt(intStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return null;
    }

    public static Long convertToLong(String longStr) {
        try {
            return Long.parseLong(longStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return null;
    }

    public static Float convertToFloat(String fStr) {
        try {
            return Float.parseFloat(fStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return null;
    }

    public static Double convertToDouble(String dStr) {
        try {
            return Double.parseDouble(dStr);
        } catch (NumberFormatException e) {
            //e.printStackTrace();
        }
        return null;
    }

    /**
     * 生产一个全球唯一ID
     * @return
     */
    public static String getGuid(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    /**
     * 返回文件大小自动返回单位,kb,mb,gb
     * @param fileSize 文件大小
     * @return
     */
    public static String getFileSize_KB_MB_GB(long fileSize){
        String result = "无法计算";
        if(fileSize>= FileUtils.ONE_GB){
            result=float2ToString(fileSize/1.0f/FileUtils.ONE_GB)+"GB";
        }
        else if(fileSize>=FileUtils.ONE_MB){
            result=float2ToString(fileSize/1.0f/FileUtils.ONE_MB)+"MB";
        }
        else{
            //KB
            result=float2ToString(fileSize/1.0f/FileUtils.ONE_KB)+"KB";
        }
        return result;
    }

}
